/*     */ package com.zimbra.cs.ldap.unboundid;
/*     */ 
/*     */ import com.unboundid.ldap.sdk.FailoverServerSet;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionOptions;
/*     */ import com.unboundid.ldap.sdk.LDAPException;
/*     */ import com.unboundid.ldap.sdk.LDAPURL;
/*     */ import com.unboundid.ldap.sdk.ServerSet;
/*     */ import com.unboundid.ldap.sdk.SingleServerSet;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.ldap.LdapConnType;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig;
/*     */ import java.net.InetAddress;
/*     */ import java.net.UnknownHostException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.LinkedHashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import javax.net.SocketFactory;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LdapServerPool
/*     */ {
/*     */   private static final String DUMMY_LDAPI_HOST = "dummy_host";
/*     */   private static final int DUMMY_LDAPI_PORT = 1;
/*     */   List<LDAPURL> urls;
/*     */   String rawUrls;
/*     */   LdapConnType connType;
/*     */   LDAPConnectionOptions connOpts;
/*     */   ServerSet serverSet;
/*     */   
/*     */   public LdapServerPool(LdapServerConfig config)
/*     */     throws LdapException
/*     */   {
/*  61 */     this.rawUrls = config.getLdapURL();
/*     */     
/*  63 */     this.urls = new ArrayList();
/*     */     
/*  65 */     String[] ldapUrls = config.getLdapURL().split(" ");
/*     */     
/*     */ 
/*  68 */     if ((config.getConnType() == LdapConnType.LDAPI) && 
/*  69 */       (ldapUrls.length > 1)) {
/*  70 */       throw LdapException.INVALID_CONFIG("can only specify one url for ldapi connection: " + this.rawUrls, null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  75 */     for (String ldapUrl : ldapUrls) {
/*     */       try {
/*  77 */         LDAPURL url = new LDAPURL(ldapUrl);
/*     */         
/*  79 */         if (LdapConnType.isLDAPI(url.getScheme()))
/*     */         {
/*  81 */           if ((url.hostProvided()) || (url.portProvided())) {
/*  82 */             throw LdapException.INVALID_CONFIG("host and port must not be specified with ldapi url: " + ldapUrl, null);
/*     */           }
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  92 */           url = new LDAPURL(url.getScheme(), "dummy_host", Integer.valueOf(1), url.getBaseDN(), url.getAttributes(), url.getScope(), url.getFilter());
/*     */         }
/*     */         
/*     */ 
/*  96 */         this.urls.add(url);
/*     */       } catch (LDAPException e) {
/*  98 */         throw LdapException.INVALID_CONFIG(e);
/*     */       }
/*     */     }
/*     */     
/* 102 */     this.connType = config.getConnType();
/* 103 */     this.connOpts = LdapConnUtil.getConnectionOptions(config);
/*     */     
/* 105 */     SocketFactory socketFactory = LdapConnUtil.getSocketFactory(this.connType, config.sslAllowUntrustedCerts());
/*     */     
/*     */     try
/*     */     {
/* 109 */       this.serverSet = createServerSet(socketFactory);
/*     */     } catch (UnknownHostException e) {
/* 111 */       throw LdapException.INVALID_CONFIG(e);
/*     */     }
/*     */   }
/*     */   
/*     */   public List<LDAPURL> getUrls() {
/* 116 */     return this.urls;
/*     */   }
/*     */   
/*     */   public String getRawUrls()
/*     */   {
/* 121 */     return this.rawUrls;
/*     */   }
/*     */   
/*     */   public LdapConnType getConnectionType() {
/* 125 */     return this.connType;
/*     */   }
/*     */   
/*     */   public ServerSet getServerSet() {
/* 129 */     return this.serverSet;
/*     */   }
/*     */   
/*     */   private ServerSet createServerSet(SocketFactory socketFactory) throws UnknownHostException {
/* 133 */     if (this.urls.size() == 1) {
/* 134 */       LDAPURL url = (LDAPURL)this.urls.get(0);
/* 135 */       if (LdapConnType.isLDAPI(url.getScheme()))
/*     */       {
/* 137 */         return new SingleServerSet(url.getHost(), url.getPort(), socketFactory, this.connOpts);
/*     */       }
/* 139 */       InetAddress[] addrs = InetAddress.getAllByName(url.getHost());
/* 140 */       if (addrs.length == 1) {
/* 141 */         if (socketFactory == null) {
/* 142 */           return new SingleServerSet(url.getHost(), url.getPort(), this.connOpts);
/*     */         }
/* 144 */         return new SingleServerSet(url.getHost(), url.getPort(), socketFactory, this.connOpts);
/*     */       }
/*     */       
/* 147 */       Set<String> uniqAddr = new HashSet();
/* 148 */       for (int i = 0; i < addrs.length; i++) {
/* 149 */         uniqAddr.add(addrs[i].getHostAddress());
/*     */       }
/* 151 */       if (uniqAddr.size() == 1) {
/* 152 */         if (socketFactory == null) {
/* 153 */           return new SingleServerSet(url.getHost(), url.getPort(), this.connOpts);
/*     */         }
/* 155 */         return new SingleServerSet(url.getHost(), url.getPort(), socketFactory, this.connOpts);
/*     */       }
/*     */       
/* 158 */       String[] hosts = new String[uniqAddr.size()];
/* 159 */       int[] ports = new int[uniqAddr.size()];
/* 160 */       int i = 0;
/* 161 */       for (String addr : uniqAddr) {
/* 162 */         hosts[i] = addr;
/* 163 */         ports[i] = url.getPort();
/* 164 */         i++;
/*     */       }
/* 166 */       if (socketFactory == null) {
/* 167 */         return new FailoverServerSet(hosts, ports, this.connOpts);
/*     */       }
/* 169 */       return new FailoverServerSet(hosts, ports, socketFactory, this.connOpts);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 174 */     Set<Pair<String, Integer>> hostsAndPorts = new LinkedHashSet();
/* 175 */     for (LDAPURL url : this.urls) {
/* 176 */       InetAddress[] addrs = InetAddress.getAllByName(url.getHost());
/* 177 */       if (addrs.length == 1) {
/* 178 */         hostsAndPorts.add(new Pair(url.getHost(), Integer.valueOf(url.getPort())));
/*     */       } else {
/* 180 */         for (int i = 0; i < addrs.length; i++) {
/* 181 */           hostsAndPorts.add(new Pair(addrs[i].getHostAddress(), Integer.valueOf(url.getPort())));
/*     */         }
/*     */       }
/*     */     }
/* 185 */     String[] hostsStrs = new String[hostsAndPorts.size()];
/* 186 */     int[] portsStrs = new int[hostsAndPorts.size()];
/* 187 */     int i = 0;
/* 188 */     for (Pair<String, Integer> pair : hostsAndPorts) {
/* 189 */       hostsStrs[i] = ((String)pair.getFirst());
/* 190 */       portsStrs[i] = ((Integer)pair.getSecond()).intValue();
/* 191 */       i++;
/*     */     }
/* 193 */     if (socketFactory == null) {
/* 194 */       FailoverServerSet serverSet = new FailoverServerSet(hostsStrs, portsStrs, this.connOpts);
/* 195 */       serverSet.setReOrderOnFailover(true);
/* 196 */       return serverSet;
/*     */     }
/* 198 */     FailoverServerSet serverSet = new FailoverServerSet(hostsStrs, portsStrs, socketFactory, this.connOpts);
/* 199 */     serverSet.setReOrderOnFailover(true);
/* 200 */     return serverSet;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/ldap/unboundid/LdapServerPool.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */